//
//  PersonInfoInputCell.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/10.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "PersonInfoInputCell.h"
#import "XXTool.h"

@implementation PersonInfoInputModel

@end

@interface PersonInfoInputCell ()<UITextFieldDelegate>

@property (nonatomic,strong) UILabel *cueLabel;

@property (nonatomic,strong) UITextField *input;

@end

@implementation PersonInfoInputCell
-(UILabel*)cueLabel
{
    if (_cueLabel==nil)
    {
        _cueLabel = [[UILabel alloc] init];
        if ([XXTool didCurrentLanguageChinese])
        {
            _cueLabel.font = [XXTool boldSystemFontOfSize:16.5f];
        }
        else
        {
            _cueLabel.font = [XXTool boldSystemFontOfSize:15];
        }
        _cueLabel.textColor = [UIColor blackColor];
        [self addSubview:_cueLabel];
    }
    return _cueLabel;
}

-(UITextField*)input
{
    if (_input==nil)
    {
        _input = [[UITextField alloc] init];
        _input.font = [XXTool systemFontOfSize:16.5f];
        _input.textColor = [UIColor blackColor];
        [_input setValue:[UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
        [_input addTarget:self action:@selector(exitEidt) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_input addTarget:self action:@selector(inputTextDidChange) forControlEvents:UIControlEventEditingChanged];
        _input.returnKeyType = UIReturnKeyDone;
        _input.delegate = self;
        [self addSubview:_input];
    }
    return _input;
}

-(void)refreshLayout:(CGRect)frame
{
    _cueLabel.frame = CGRectMake(20
                                 , 0
                                 , 105
                                 , frame.size.height);
    _input.frame = CGRectMake(_cueLabel.frame.size.width+_cueLabel.frame.origin.x
                              , 0
                              , frame.size.width-(_cueLabel.frame.size.width+_cueLabel.frame.origin.x)
                              , frame.size.height);
}

-(void)exitEidt
{
    [_input resignFirstResponder];
}

-(void)inputTextDidChange
{
    if ([_delegate respondsToSelector:@selector(inputTextDidChangeWithPersonInfoInputCell:changeText:)])
    {
        [_delegate inputTextDidChangeWithPersonInfoInputCell:self changeText:_input.text];
    }
}

#pragma mark textfieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL is=false;
    if ([_delegate respondsToSelector:@selector(personInfoInputCell:textField:shouldChangeCharactersInRange:replacementString:)])
    {
        is = [_delegate personInfoInputCell:self
                                  textField:textField
              shouldChangeCharactersInRange:range
                          replacementString:string];
    }
    return is;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(personInfoInputCell:textFieldDidBeginEditing:)])
    {
        [_delegate personInfoInputCell:self textFieldDidBeginEditing:textField];
    }
}

-(void)setCellWithData:(PersonInfoInputModel *)data
{
    if (data.isRequired)
    {
        NSMutableAttributedString *cue = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@*",data.cueString]];
        [cue addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:196/255.0 green:0 blue:24/255.0 alpha:1.0]} range:NSMakeRange(cue.length-1, 1)];
        self.cueLabel.attributedText = cue;
        self.input.placeholder = NSLocalizedString(@"必填", nil);
    }
    else
    {
        self.cueLabel.text = data.cueString;
        self.input.placeholder = NSLocalizedString(@"选填", nil);
    }
    _input.enabled = !data.isLeibie;
    _input.text = data.inputString;
}

-(void)setLeibieInfo:(NSString*)info{
    _input.text = info;
}

@end
